package com.shen.show.view

import android.content.Context
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.util.Log
import android.view.SurfaceHolder
import com.shen.show.render.FFRender

class FFPlayView: GLSurfaceView, SurfaceHolder.Callback{
    fun setAspectRatio(width: Int, height: Int) {
        Log.d(
           TAG,
            "setAspectRatio() called with: width = [$width], height = [$height]"
        )
        require(!(width < 0 || height < 0)) { "Size cannot be negative." }

        mRatioWidth = width
        mRatioHeight = height
        requestLayout()
    }
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)

        if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height)
        } else {
            if (width < height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth)
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height)
            }
        }
    }
    var mRatioWidth=0;
    var mRatioHeight=0;

    private  val TAG = "FfPlayView"
    override fun surfaceCreated(holder: SurfaceHolder) {
        super.surfaceCreated(holder)
        Log.d(TAG,"surfaceCreated")
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        super.surfaceChanged(holder, format, width, height)
        Log.d(TAG,"surfaceChanged")
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        super.surfaceDestroyed(holder)
        Log.d(TAG,"surfaceDestroyed")
    }
    constructor(context:Context,attrs: AttributeSet):super(context,attrs){
        Log.d(TAG,"constructor")
        iniView(context)
    }

//    constructor(context:Context,attrs:android.util.AttributeSet,defStyleAttr:Int):super(context,attrs,defStyleAttr){
//        iniView(context)
//    }
    constructor(context:Context):super(context){
        Log.d(TAG,"constructor")
        iniView(context)
    }

    fun iniView(context:Context){
        setEGLContextClientVersion(3);
        setRenderer(FFRender())
        renderMode= RENDERMODE_CONTINUOUSLY;
        holder.addCallback(this)
//        mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    }

}